package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import database.DBManager;
import DAO.MeasurementsDao;
import model.Measurement;

/**
 * Servlet implementation class TestServlet
 */
public class CurrentMeasurements extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private DBManager myDb;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CurrentMeasurements() {
		super();

	}

	public void init(ServletConfig servletconfig) {
		// Initialisatie
		myDb = (DBManager) servletconfig.getServletContext().getAttribute(
				"dbmanager");
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		
		MeasurementsDao md = new MeasurementsDao(myDb);
		ArrayList<Measurement> latestMeasurements = new ArrayList<Measurement>();
		for(int i = 1; i <= 5; i++){
			Measurement measurement = md.getLatestMeasurementByID(i);
			if(measurement != null){
				latestMeasurements.add(measurement);
			}
		}	
		
		out.print("<table id=hor-minimalist-a>" +
		         		"<tr>" +
		         			"<th scope=col> Temperatuur</th>" +
		         			"<td>" + latestMeasurements.get(3).getValue() + "</td>" +
		         		"</tr>" +
		         		"<tr>" +
		         			"<th scope=col> Luchtvochtigheid</th>" +
		         			"<td>" + latestMeasurements.get(4).getValue() + "</td>" +
		         		"</tr>" +
		         		"<tr>" +
		         			"<th scope=col> Deur</th>");
		
		if(latestMeasurements.get(2).getValue() == 0){
			out.print("<td><img src='images/dicht.png' width='40' height='40'/></td>");
 		} else {
 			out.println("<td><img src='images/open.png' width='40' height='40'/></td>");
 		} 
		
		out.print("</tr> <tr> <th scope=col>Licht</th>");
		
		if(latestMeasurements.get(1).getValue() == 0){
			out.println("<td><img src='images/uit.png' width='30' height='40'/></td>");
 		} else {
 			out.println("<td><img src='images/aan.png' width='30' height='40'/></td>");
 		} 
		
		out.println("</tr> <tr>	<th scope=col>Laatste beweging</th>" +
				"<td>" + latestMeasurements.get(0).getMeasureDate() + "</td>" +
		"</tr>");
		
		out.print("</td>" +
	 		"</tr>" +
	 	"</table>");
		
//		out.print("<br> <br> <form method=" + "post" + "action=" + "SensorServlet?page=lamp"+ ">");
//
//		if (latestMeasurements.get(1).getValue() == 0) {
//			out.print("<input type=" + "submit" + "value=" + "Zet de lamp aan" + "</form>");
//		} else {
//			out.print("<input type=" + "submit" + "value=" + "Zet de lamp uit" + "/> </form>");
//		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

	}
}